<template>
  <section class="container">
    <transition name="slide-fade">
      <div class="screen"  v-if="stepy === 1">
        <div class="view">
          <div class="phone_bg">
            <div class="phone_top">
              <img :src="require('../styles/asset/inviterphone.png')" alt="">
            </div>
            <div class="phone_btm">
              <div class="phone">
                <div class="text-shadow">
                  <input type="text" placeholder="请输入手机号码" v-model="phone" ref="phone" />
                </div>
              </div>
              <div class="phone_btn">
                <button class="phone_next" v-on:click="phoneNext()">下一步</button>
                <!--<span>完成注册后获得5张优惠券</span>-->
              </div>
            </div>
          </div>
        </div>
      </div>
    </transition>
    <transition name="slide-fade">
      <div class="screen"  v-if="stepy === 2">
        <div class="view">
          <div class="sendCode_bg">
            <div class="sendCode_top">
              <img :src="require('../styles/asset/inviterphone.png')" alt="">
            </div>
            <div class="sendCode_btm">
              <div class="pop-ipt" v-show="imgShow">
                <div class="text-shadow">
                  <input type="text" placeholder="请输入图形验证码" v-model="imgCode" ref="code"/>
                  <img :src="imgCodeSrc" @click="getImgCode" class="img-code">
                </div>
              </div>
              <div class="sendCode">
                <div class="text-shadow">
                  <input type="text" placeholder="请输入验证码" v-model="code" ref="code"/>
                  <button class="post_send" v-on:click="sendCode()" :disabled="!show">
                    <i class="code_line"></i>
                    <span v-show="show">获取验证码</span>
                    <span v-show="!show" class="count">{{count}}s</span>
                  </button>
                </div>
              </div>
              <div class="sendCode_btn">
                <button class="sendCode_next" v-on:click="sendCodeNext()">下一步</button>
                <!--<span>完成注册后获得5张优惠券</span>-->
              </div>
            </div>
          </div>
        </div>
      </div>
    </transition>
    <transition> name="slide-fade">
      <div class="screen"  v-if="stepy === 3">
        <div class="view">
          <div class="down_bg">
            <div class="appdown_top">
              <img :src="require('../styles/asset/appdown_top.jpg')" alt="">
            </div>
            <div class="appdown_btm">
              <img :src="require('../styles/asset/appdown_btm.jpg')" alt="" />
              <button class="app_btn"><a @click="downApp()">立即下载</a></button>
            </div>
          </div>
        </div>
      </div>
    </transition>
    <simplert  ref="simplert"></simplert>
  </section>
</template>

<script>
  import Simplert from '../../components/dialog/dialog.vue'

  var filters = {
    parseURL:function(url) {
      var a = document.createElement('a');
      a.href = url;
      return {
        params: (function () {
          if(a.href.split('?').length > 2) {
            let ret = {},
              param = a.href.split('?')[2],
              seg = param.split('&'),
              len = seg.length,
              i = 0,
              s;
            for (; i < len; i++) {
              if (!seg[i]) {
                continue;
              }
              s = seg[i].split('=');
              ret[s[0]] = s[1];
            }
            return ret;
          } else {
            let ret = {},
              param = a.href.split('?')[1],
              seg = param.split('&'),
              len = seg.length,
              i = 0,
              s;
            for (; i < len; i++) {
              if (!seg[i]) {
                continue;
              }
              s = seg[i].split('=');
              ret[s[0]] = s[1];
            }
            return ret;
          }
        })()
      }
    }
  }

  export default {
    data() {
      return {
        token:'',
        areaCode:'',
        imgCodeSrc: '',
        imgCode:'',
        imgShow:false,
        picKey:'',
        stepy: 1,
        phone: '',
        code: '',
        show: true,
        count: '',
        timer: false,
        inviteCouponId:'',
        regCouponId: '',
        inviterPhone: ''
      }
    },
    created() {

    },
    mounted() {
      // const that = this;
      const url = window.location.href;
      this.inviterPhone = filters.parseURL(url).params.inviterPhone;
      this.areaCode = filters.parseURL(url).params.areaCode;
      if(this.areaCode == undefined) {
        this.areaCode = '';
      }
      // this.$util.getUserToken().then(token=>{
      //   that.token = token;
      //   that.getHeadImg();
      // }).catch(e=>{
      //   this.state = false;
      // })
      this.getInviteCouponId(1);
      this.getRegCouponId(2);
      //获取图形验证
      this.checkImgCode();
    },
    components: {
      Simplert
    },
    methods: {
      getInviteCouponId(type) {
        let _this = this;
        let IdArr = [];
        this.$api.get('/userAccount/getInviteCoupon?type='+ type, {}, s=> {
          s.module.forEach(function (item) {
            IdArr.push(parseInt(item.id));
          });
          _this.inviteCouponId += IdArr.splice(',');
        });
      },
      getRegCouponId(type) {
        let _this = this;
        let IdArr = [];
        this.$api.get('/userAccount/getInviteCoupon?type='+ type, {}, s=> {
          s.module.forEach(function (item) {
            IdArr.push(parseInt(item.id));
          });
          _this.regCouponId += IdArr.splice(',');
        });
      },
      open (title, confirmBtn,customConfirmBtnText,onConfirm,type,onClose,message) {
        let obj = {
          title: title,
          useConfirmBtn: confirmBtn,
          customConfirmBtnText: customConfirmBtnText,
          onConfirm:onConfirm,
          type: type,
          onClose:onClose,
          message:message
        };
        this.$refs.simplert.openSimplert(obj);
      },
      phoneNext() {
        if(this.phone === '')
        {
          this.open('请输入手机号码',false,'','','fade_alert',function(){
        });
          return false;
        }
        let pat = /^1\d{10}$/;
        if (!pat.test(this.phone)) {
          this.open('请输入有效的手机号码',false,'','','fade_alert',function(){});
          return false;
        }
        if(this.phone === this.inviterPhone) {
          this.open('不能邀请自己',false,'','','fade_alert',function(){});
          return false;
        }
        // this.checkImgCode();
        this.stepy++;
      },
      checkImgCode () { // 判断图形验证码开关
        let _this = this;
        this.$api.post('/protocol/v1/protocolAddr', {}, s => {
          // console.log(s);
          // s.module.forEach(function(e,index){
            if (s.module.hasOwnProperty("smsPicSwitch")) {
                if (s.module.smsPicSwitch == 1){
                // 开
                  _this.getImgCode();
                  _this.imgShow=true
                } else {
                  // 关
                  _this.imgShow=false
                }
            }else{
              _this.imgShow=false
            }

          // })
        },f=>{
          this.open('获取图形失败',false,'','','fade_alert',function(){});
        })
      },
      getImgCode () { // 获取图形验证码
        let _this = this;
        this.$api.get('/other/v1/getRegVCode', {}, s => {
          _this.imgCodeSrc = s.module.imgSrc;
          _this.picKey = s.module.picKey;
        }, f => {
        });
      },
      sendCodeNext() {
//        let root = process.env.MAIN_ROOT;
//        if(root.indexOf('d-h5')>=0)
//        {
//          //券id改为全国券
//          this.couponId = '89'
//        }else{
//          this.couponId = '75'
//        }
        if(this.code === '')
        {
          this.open('请输入验证码',false,'','','fade_alert',function(){});
          return false;
        }
        let pat = /^\d{6}$/;
        if (!pat.test(this.code)) {
          this.open('请输入正确的验证码',false,'','','fade_alert',function(){});
          return false;
        }
        this.$api.post('/user/v1/inviteReg?inviterPhone='+this.inviterPhone+'&phone='+this.phone +'&inviteCouponId=' + this.inviteCouponId +'&regCouponId='+this.regCouponId + '&captchaCode=' + this.code,{},s=>{
          this.open('注册成功',false,'','','fade_success',function(){});
          this.stepy++;
        },f=>{
          this.open(f.errorMessage,false,'','','fade_alert',function(){
          });
        });
      },
      sendCode(){
        const time = 60;
        if (!this.timer) {
          this.timer = true;
          this.$api.post('/sendCode/v1/sendH5RegSms?phone='+this.phone+'&vCode='+this.imgCode+'&picKey='+this.picKey,{},s=>{
            this.open('验证码发送成功',false,'','','fade_success',function(){

            });
            this.count = time;
            this.show = false;
            const countTime = setInterval(() => {
              if (this.count > 0 && this.count <= time) {
                this.count--;
              } else {
                this.show = true;
                clearInterval(countTime);
                this.timer = false;
              }
            }, 1000)
          },f=>{
            this.timer = false;
            this.open(f.errorMessage,false,'','','fade_alert',function(){

            });
            return;
          });
        }
      },
      downApp() {
        let userAgentObj = navigator.userAgent;
        if(userAgentObj.match(/Android/i)){
          window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919';
        }else if(userAgentObj.match(/iPhone/i)) {
          window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8";
        }
        return;
      }
    }
  }
</script>

<style scoped >
  @import "../../activities/styles/styles.css";
  body {
    background: #eeeeee;
  }
  .container,.screen,.view{
    width: 100%;
    height: 100%;
    background: #ffffff;
  }

  .phone_bg,.sendCode_bg,.down_bg {
    max-width: 750px;
    background-color: #ffffff;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
  }
  .phone_top,.sendCode_top {
    width: 100%;
  }
  .phone_top img,
  .sendCode_top img{
    width: 100%;
  }
  .phone_btm,
  .sendCode_btm{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }
  .phone_btm .phone,
  .sendCode_btm .sendCode,.pop-ipt{
    width: 100%;
    height: 60px;
    text-align: center;
    margin-top: -30px;
    line-height: 60px;
  }
  .sendCode_btm .sendCode{
    margin-top: 10px;
  }
  .text-shadow {
    display: inline-block;
    width: 90%;
    height: 100%;
    /*box-shadow: 0 3px 8px #AAAAAA;*/
    box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.2);
    border-radius: 3px;

  }
  .img-code{
    position: absolute;
    top: 0;
    right: 0;
    width: 2.25rem;
    height: 100%;
    font: 0.30rem/1rem "Microsoft YaHei";
    color: #fff;
    border: none;
    outline: none;
}
  .phone input,
  .sendCode input,.pop-ipt input{
    float: left;
    padding: 0;
    width: 100%;
    height: 100%;
    border-radius: 3px;
    outline: none;
    font-size: 15px;
    text-indent: 15px;
    border: none;
    line-height: 30px;
  }
  .phone_btm .phone_btn,
  .sendCode_btm .sendCode_btn{
    width: 100%;
    text-align: center;
    height: 60px;
    line-height: 60px;
    margin-top: 15px;
  }
  .phone_btn button,
  .sendCode_btn button{
    display: block;
    margin-left: 50%;
    transform: translateX(-50%);
    float: left;
    width: 90%;
    height: 100%;
    border: none;
    border-radius: 3px;
    outline: none;
    background-color: #ff5d2e;
    color: #ffffff;
    font-size: 16px;
    box-shadow: 0 3px 8px rgba(255,93,46,0.4);
  }
  .phone_btn span,
  .sendCode_btn span{
    display: inline-block;
    font-size: 18px;
  }
  .sendCode input ,.pop-ipt input{
    position: relative;
  }
  .code_line {
    display: inline-block;
    width: 1px;
    height: 30px;
    background: #000000;
    float: left;
  }
  .sendCode .post_send {
    width: 100px;
    height: 60px;
    line-height: 30px;
    border:none;
    background: none;
    position: absolute;
    top:0;
    right: 0;
    outline: none;
    text-align: center;
    font-size: 16px;
  }
  .down_bg .appdown_top {
    width: 100%;
  }
  .down_bg .appdown_btm {
    width: 100%;
  }
  .down_bg img {
    width: 100%;
  }
  .appdown_btm {
    position: relative;
  }
  .appdown_btm button {
    position: absolute;
    top: 50%;
    left: 50%;
    border: none;
    width: 200px;
    height: 50px;
    border-radius: 30px;
    background-color: #ff5d2f;
    box-shadow: 0 3px 10px rgba(255,93,47,0.4);
    outline: none;
    transform: translate(-50%,-50%);
  }
  .appdown_btm button a {
    display: block;
    color: #ffffff;
    font-size: 18px;
  }
</style>
